package cn.data.collection.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import cn.data.collection.constant.CollectionParaConstant;
import cn.data.collection.constant.CommonData;
import cn.data.collection.constant.InitPropertiesConstant;
import cn.data.collection.constant.NumberConstant;
import cn.data.collection.constant.RegisterKey;
import cn.data.collection.constant.ThreadCommonData;
import cn.data.collection.interceptor.AutoLimitCollectionThread;
import cn.data.collection.util.encrypt.CryptoUtil;

public class CollectionHelper {

	public String satartCollection() {
		try {
			new ProgramRegister().init();
			PropertiesUtils.getInstance();
		} catch (Exception e) {
			LoggerUtils.getInstance().error("内部发生了错误!");
			return "内部发生了错误!";
		}
		Object testBatValidate = ProgramRegister.REGIESTDATA.get(RegisterKey.TEST_BAT_VALIDATE);
		if (testBatValidate != null && (boolean) testBatValidate == true) {
			LoggerUtils.getInstance().error("测试版本已不能使用,请使用正式版本!");
			return "测试版本已不能使用,请使用正式版本!";
		}

		Connection conn = null;

		try {
			conn = DBConnection.getConnection();
			LoggerUtils.getInstance().error("数据库检查成功：" + DateUtils.nowDate(DateUtils.DATETIME_FORMATE));
		} catch (SQLException e) {
			LoggerUtils.getInstance().error("数据库检查失败,采集系统停止：" + DateUtils.nowDate(DateUtils.DATETIME_FORMATE));
			return "数据库检查失败,采集系统停止：" + DateUtils.nowDate(DateUtils.DATETIME_FORMATE);
		} finally {
			DBConnection.releaseConnection(conn, null, null);
		}
		
		try {
			initDama();
			LoggerUtils.getInstance().error("初始打码成功：" + DateUtils.nowDate(DateUtils.DATETIME_FORMATE));
		} catch (Exception e) {
			LoggerUtils.getInstance().error("初始打码失败：" + DateUtils.nowDate(DateUtils.DATETIME_FORMATE));
			return "初始打码失败,采集系统停止：" + DateUtils.nowDate(DateUtils.DATETIME_FORMATE);
		}


		CollectionParaConstant.autoRunFlag = true;
		initCompany(DateUtils.nowDate(null));
		try {
			int collectThreadNum = Integer.valueOf(PropertiesUtils.getInstance().getINITDATA().get(InitPropertiesConstant.COLLECTION_THREAD_NUM).toString());
			ExecutorService objExecutorService = Executors.newFixedThreadPool(collectThreadNum+NumberConstant.TWO);
			
			for (int i = 0; i < collectThreadNum; i++) {
				objExecutorService.execute(new AutoLimitCollectionThread(i));
			}
		} catch (Exception e) {
			LoggerUtils.getInstance().error(e);
		}
		return "启动成功";
	}
	
	private void initDama() throws Exception{
		CommonData.DAMA_SOFTWARE_ID = CryptoUtil.decode(PropertiesUtils.getInstance().getINITDATA().get(InitPropertiesConstant.DAMA_SOFTWARE_ID).toString());
		CommonData.DAMA_USER_NAME = CryptoUtil.decode(PropertiesUtils.getInstance().getINITDATA().get(InitPropertiesConstant.DAMA_USER_NAME).toString());
		CommonData.DAMA_PASSWORD = CryptoUtil.decode(PropertiesUtils.getInstance().getINITDATA().get(InitPropertiesConstant.DAMA_PASSWORD).toString());
	}

	private void initCompany(String date) {
		ResultSet rs = null;
		String selectSQL = "select shoulihao,companyName from company where createDateTime like '" + date + "%'";
		Statement st = null;
		Connection conn = null;
		LoggerUtils.getInstance().error("初始化的公司数据SQL:" + selectSQL);
		try {
			conn = DBConnection.getConnection();
			st = conn.createStatement();
			rs = st.executeQuery(selectSQL);
			while (rs.next()) {
				ThreadCommonData.HAVED_COLLECTION_COMPANY.put(rs.getString(1), rs.getString(2));
			}
			if (ThreadCommonData.HAVED_COLLECTION_COMPANY.size() == 0) {
				Statement delTBST = conn.createStatement();
				delTBST.executeUpdate("truncate table company");
				DBConnection.releaseConnection(null, delTBST, null);
			}
		} catch (SQLException e) {
			LoggerUtils.getInstance().error("初始化公司数据出错:" + e);
		} finally {
			DBConnection.releaseConnection(conn, st, rs);
		}
	}
}
